package yanglib.application.metadata.message;

import yanglib.message.Message;
import yanglib.message.MessageDeserializer;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * Created with IntelliJ IDEA.
 * User: user
 * Date: 9/6/14
 * Time: 8:16 PM
 * To change this template use File | Settings | File Templates.
 */
public class MetadataMessageDeserializer implements MessageDeserializer {
    public static final int PUT_MESSAGE = 1;
    public static final int PUT_REPLY_MESSAGE = 2;
    public static final int GET_MESSAGE = 3;
    public static final int GET_REPLY_MESSAGE = 4;

    @Override
    public Message deserialize(byte[] bytes, int offset, int len) throws IOException {
        ByteBuffer tmp = ByteBuffer.wrap(bytes, offset, len);
        int type = tmp.getInt();
        tmp.rewind();
        switch(type){
            case PUT_MESSAGE:
                return new PutMessage(tmp);
            case PUT_REPLY_MESSAGE:
                return new PutReplyMessage(tmp);
            case GET_MESSAGE:
                return new GetMessage(tmp);
            case GET_REPLY_MESSAGE:
                return new GetReplyMessage(tmp);
            default:
                throw new IOException("Unknown message type "+type);
        }

    }


}
